<template>
	<view>
		<view class="pageTitle">
			忘记密码
		</view>
		<view class="loginBlock">
			<u-field v-model="phone" @input="phoneChange" placeholder="请输入账号"
				:error-message="errorMessagePhone"></u-field>
			<u-field v-model="verify" @input="verifyChange" type="number" placeholder="请输入验证码"
				:error-message="errorMessageVerify">
				<view :class='time == 0?"getVerify green":"getVerify"' slot="right" @click="getVerify">
					{{time == 0?"获取验证码":time+"s"}}
				</view>
			</u-field>
			<u-field v-model="password" @input="passwordChange" type="password" placeholder="至少6位密码,包含数字字母"
				:error-message="errorMessagePassword">
			</u-field>
			<u-field v-model="passwordC" @input="passwordCChange" type="password" placeholder="再次输入密码"
				:error-message="errorMessagePasswordC">
			</u-field>
			<u-button type="success" shape="circle" :custom-style="buttonStyle" @click="userLogin">提交</u-button>
		</view>
		<u-toast ref="uToast" />
		<u-no-network></u-no-network>
	</view>
</template>

<script>
	import {
		buttonStyle
	} from "@/utils/buttonStyle.js";
	export default {
		data() {
			return {
				buttonStyle,
				phone: "",
				password: "",
				errorMessagePhone: "",
				errorMessagePassword: "",
				errorMessageVerify: "",
				errorMessagePasswordC: "",
				passwordC: "",
				verify: "",
				time: 0,
				timer: "",
				canVerify: true,
				codeType: 0, //0短信 1邮箱
			}
		},
		onShow() {

		},
		methods: {
			cutDown() {
				this.time--
				if (this.time == 0) {
					this.canVerify = true
					clearInterval(this.timer)
				}
			},
			getVerify() {
				let that = this
				if (!that.canVerify) {
					return false
				}
				if (that.phone == "") {
					that.errorMessagePhone = "请输入您的手机号";
					return;
				}
				that.canVerify = false
				let params = {
					mobile: that.phone,
					lang: ""
				}
				that.$api.getVerify(params).then((res) => {
					if (res.resultCode == 0) {
						that.time = 90
						that.timer = setInterval(that.cutDown, 1000)
						uni.showToast({
							title: '验证码已发送',
							icon: "none"
						})
					} else {
						that.canVerify = true
					}
				})
			},
			verifyChange(e) {
				if (e != "") {
					this.errorMessageVerify = ""
				}
			},
			phoneChange(e) {
				if (e != "") {
					this.errorMessagePhone = ""
				}
			},
			passwordCChange(e) {
				if (e != "") {
					this.errorMessagePasswordC = ""
				}
			},
			passwordChange(e) {
				if (e != "") {
					this.errorMessagePassword = ""
				}
			},
			userLogin() {
				let that = this
				if (that.phone == "") {
					that.errorMessagePhone = "请输入您的手机号";
					return;
				}
				if (that.password == "") {
					that.errorMessagePassword = "请输入您的密码";
					return;
				}
				if (that.password != that.passwordC) {
					that.errorMessagePasswordC = "密码不一致";
					return;
				}
				if (that.verify == "") {
					that.errorMessageVerify = "请输入短信验证码";
					return;
				}
				let params = {
					mobile: that.phone,
					code: that.verify,
					newPwd: that.password,
					codeType: that.codeType
				}
				that.$api.resetPwd(params).then((res) => {
					uni.showToast({
						title: '密码修改成功',
						icon: "success",
						duration: 2000,
						success: () => {
							setTimeout(function() {
								uni.redirectTo({
									url: '/pages/login/index'
								})
							}, 2000)
						}
					})
				})
			}
		}
	};
</script>

<style scoped lang="less">

</style>